﻿using System;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Common;
using QDT.Common.Data;
using QDT.Common.Plugins.Email;
using QDT.MVC.Security;
using QDT.P2B.Application;
using QDT.P2B.Domain.Config;
using QDT.P2B.Domain.Messages;
using QDT.Services;

namespace QDT.Web.Admin.Controllers {
    [SecurityModule(Name = "邮件管理")]
    public class EmailController : BaseController {
        [SecurityNode(Name = "邮件配置页")]
        public ActionResult Config() {
            var config = SettingManager.Get<EmailConfig>();
            return View(config);
        }

        [SecurityNode(Name = "邮件配置")]
        public ActionResult PostConfig(string name, string smtpServer, int smtpPort, string smtpUserName, string smtpPassword, string subjectPrefix, bool isSSL)
        {
            var config = SettingManager.Get<EmailConfig>();

            config.Name = name;
            config.SmtpServer = smtpServer;
            config.SmtpPort = smtpPort;
            config.SmtpUserName = smtpUserName;
            config.SmtpPassword = smtpPassword;
            config.SubjectPrefix = subjectPrefix;
            config.IsSSL = isSSL;

            SettingManager.Save(config);

            return Success("操作成功");
        }
         [SecurityNode(Name = "邮件测试")]
        public ActionResult Test(string email) {

            var msgQueue = new MsgQueueFactory().CreateMailTest(email);

            DbProvider.Add(msgQueue);

            DbProvider.SaveChanges();

            return Success("操作成功");
        }

        [SecurityNode(Name = "发件箱")]
        public ActionResult Index(int? page, GridSortOptions sort, string receiver, string content)
        {
            const int pageSize = 20;
            var pageNumber = page ?? 1;
            ViewData["sort"] = sort;

            if (sort.Column == null)
            {
                sort.Column = "Id";
                sort.Direction = SortDirection.Ascending;
            }

            int type = (int)MsgType.Email;

            var source =
                DbProvider.D<MsgQueue>().Where(x => x.InnerType == type && x.IsSend).OrderBy(sort.Column, sort.Direction == SortDirection.Descending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        [SecurityNode(Name = "邮件队列")]
        public ActionResult Queue(int? page, GridSortOptions sort, string receiver, string content)
        {
            const int pageSize = 20;
            var pageNumber = page ?? 1;
            ViewData["sort"] = sort;

            int type = (int)MsgType.Email;

            var source =
                DbProvider.D<MsgQueue>().Where(x => x.InnerType == type && !x.IsSend).OrderBy(sort.Column, sort.Direction == SortDirection.Descending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult Content(int id) {
            var content = DbProvider.GetByID<MsgQueue>(id);
            return Content(content.Content);
        }

        public ActionResult Resend(int id) {
            var content = DbProvider.GetByID<MsgQueue>(id);
            if (content == null) return Error("邮件不存在");
            content.IsSend = false;
            content.IsSuccess = false;
            content.Result = "";
            DbProvider.SaveChanges();
            return Success("操作成功");
        }
    }
}